// pages/nianhao/nianhao.js
// pages/jieQi/jieQi.js
const log = require('../log/log.js')
const sizhu = require('../sizhu/sizhu.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        scrHeight: 400,
        showList: false,
        showName: false
    },
    onShareAppMessage: function () {
        return {
            title: '分享六爻策给你的伙伴吧',
            path: '/pages/index/index',
            imageUrl: '/images/6YCSHARE.png'
        }
    },
    autoStyle: function () {
        var systeminfo = wx.getSystemInfoSync();
        log.info('nianHao-autoStyle-获取系统参数', systeminfo)

        //px为单位
        var sW = systeminfo.screenWidth;
        var sH = systeminfo.screenHeight

        var rate = 750 / sW;
        console.log(rate);

        //rpx为单位
        var rW = parseInt(sW * rate)
        var rH = parseInt(sH * rate)

        //屏幕长宽比
        var hwNum = sH / sW

        //pad的参数
        var pam = 1
        if (hwNum < 1.4) {
            pam = 0.72
        }

        var scrHeight = parseInt(rH * 0.65)

        this.setData({
            scrHeight: scrHeight,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        this.autoStyle()

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    copy: function () {
        wx.setClipboardData({  //设置要复制的内容
            data: this.data.nianHao,   //这里是要复制的内容，可使用动态变量代替
            success: res => {
            }
        })
    },
    copyOne: function (e) {
        console.log(e.currentTarget.dataset.id);
        wx.setClipboardData({  //设置要复制的内容
            data: e.currentTarget.dataset.id,   //这里是要复制的内容，可使用动态变量代替
            success: res => {
            }
        })
    },
    getInputValue: function (e) {
        this.setData({
            queryInput: e.detail.value
        })
    },
    query: function () {
        var inputVal = this.data.queryInput
        var reg = /^-?\d+$/;
        if (!inputVal) {
            wx.showToast({
                title: '请输入年份或关键字查询',
                icon: 'none',
                duration: 1500
            })
            return;
        } else if ((inputVal != "" && reg.test(inputVal)) && (inputVal < -2100 || inputVal > 2050)) {
            wx.showToast({
                title: '年份范围-2100 - 2050',
                icon: 'none',
                duration: 1500
            })
            return;
        } else if (inputVal) {
            if (reg.test(inputVal)) {
                var year = parseInt(inputVal)

                //获取年号
                var nianHao = sizhu.nianHao(year)
                console.log(nianHao);
                this.setData({
                    showName: false,
                    showList: true,
                    nianHao: nianHao
                })
            } else {
                var name = inputVal

                //获取年号列表
                var nianHaoList = sizhu.getNHByName(name)
                if (nianHaoList.length > 0) {
                    this.setData({
                        showName: true,
                        showList: false,
                        nianHaoList: nianHaoList
                    })
                }else{
                    wx.showToast({
                        title: '未找到对应的年号信息',
                        icon: 'none',
                        duration: 1500
                    })
                }
            }
        }
    },
    adLoad() {
        log.info('小程序视频广告加载成功')
        log.addFilterMsg('adLoad-NH')
    },
    adError(err) {
        log.info('小程序视频广告加载失败', err)
        log.addFilterMsg('adError-NH')
    },
    adClose(){
        log.info('小程序视频广告关闭')
        log.addFilterMsg('adClose-NH')
    }
})